﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNNspot.LiveChat.DataModel
{
    public partial class Message
    {
        public static void AddMessage(int portalId, int sessionId, int? agentId, int? clientId, string body)
        {
            var message = new Message
            {
                PortalId = portalId,
                SessionId = sessionId,
                AgentId = agentId,
                ClientId = clientId,
                Body = body
            };

            message.Save();
        }

        public static bool ExistsByBody(int portalId, int sessionId, string body)
        {
            var q = new MessageQuery();
            q.Where(q.PortalId == portalId && q.SessionId == sessionId && q.Body == body);

            var messages = new MessageCollection();
            messages.Load(q);

            return messages.Count > 0;
        }
    }

    public partial class MessageCollection
    {
        public static List<Message> GetMessagesAfterMessageId(int portalId, int sessionId, int messageId)
        {
            var q = new MessageQuery();
            q.Where(q.PortalId == portalId && q.SessionId == sessionId && q.MessageId > messageId);

            var messages = new MessageCollection();
            messages.Load(q);

            return messages;
        }
    }
}
